<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>微信登录回调页面</title>
    <script type="text/javascript" src="plugins/vue/dist/vue.js"></script>
    <script type="text/javascript" src="plugins/axios/dist/axios.js"></script>
    <script type="text/javascript" src="script/common.js"></script>
</head>
<body>
<div id="my-div"></div>
<script type="text/javascript">
    new Vue({
        el:"#my-div",
        mounted() {
            // 获取url中的code，并调用后端接口把code传递到后端
            let paramObj = urlToObj(location.href);
            console.log("url解析",paramObj)
            this.$http.post("login/wechat",paramObj)
                .then(result => {
                    result = result.data;
                    if (result.success) {
                        if (result.message === "nobind") {
                            // 没有查询到微信用户信息，需要绑定账户信息
                            let {access_token,openid} = result.data;
                            // 跳转到绑定账户页面
                            location.href = "/binder.html?access_token="+access_token+"&openid="+openid;
                        } else {
                            // 获取成功并找到了用户信息，直接登录,并存储用户信息和token
                            localStorage.setItem("token",result.data.token);
                            localStorage.setItem("loginUser",JSON.stringify(result.data.loginUser))
                            this.notify.success({title: "登录成功",message:"恭喜你登录成功",type:"success"});
                            // 跳转到首页
                            location.href="index.html"
                        }
                    } else {
                        // 业务后端出错
                        alert(result.message)
                    }
                })
                .catch(result => {
                    alert(result);
                })
        },
    })
</script>
</body>
</html>